//
//  ToolVM.swift
//  ZJTools
//
//  Created by 万孟超 on 2023/10/26.
//

import Foundation


struct ToolVM {
    
    enum TimeError: Error {
        case invalidInput(String) //包含无效的字符
    }
    
    enum Tool {
        enum TimeType {
            case timeInterval
            case time
        }
        
        enum ColorType {
            case hex
            case ten
        }
    }
    
    
    func timeChange(_ type: Tool.TimeType, convert str: String) throws -> String {
        if type == .timeInterval {
            if str.isEmpty {
                throw TimeError.invalidInput("你还没有输入时间戳")
            }else{
                if str.isTimestamp {
                    return str.toDateString
                }else{
                    throw TimeError.invalidInput("叫你输入时间戳")
                }
            }
        }else{
            if str.isEmpty {
                throw TimeError.invalidInput("你还没有输入时间")
            }else{
                if str.count != 19 {
                    throw TimeError.invalidInput("时间格式为yyyy-MM-dd HH:mm:ss")
                }else{
                    return str.toTimeIntervalString
                }
            }
        }
    }
    
    func colorChange(_ type:Tool.ColorType, r: inout String, g: inout String, b: inout String, hex: inout String) {
        if type == .ten {
            hex = r.toInt.hexString + g.toInt.hexString + b.toInt.hexString
        }else{
            r = hex.hexToTen.0
            g = hex.hexToTen.1
            b = hex.hexToTen.2
        }
    }
}
